﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BobcatUtility
{
    public partial class IconStripControl : UserControl
    {
        #region Events

        /// <summary>
        /// Data passed with events raise by this control.
        /// </summary>
        public class Args : EventArgs
        {
            public string ID { get; set; }
        }

        /// <summary>
        /// Event raised when the selection changes.
        /// </summary>
        public event EventHandler<Args> SelectionChangedEvent;

        #endregion

        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public IconStripControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Adds a new icon to the control.
        /// </summary>
        public void addIcon(Image image, string strLabel, string strID)
        {
            // We create a new icon-control...
            IconControl ic = new IconControl();
            ic.Location = new Point(SPACING, m_iNextIconYPosition);
            ic.setIcon(image, strLabel, strID);
            Controls.Add(ic);
            m_IconControls.Add(ic);

            // We register for events from it...
            ic.ClickedEvent += onIconClicked;

            // We work out the position of the next icon...
            m_iNextIconYPosition += ic.Height;
            m_iNextIconYPosition += SPACING;
        }

        /// <summary>
        /// We set the selected icon to be the one at the index passed in.
        /// </summary>
        public void setSelection(int iIndex)
        {
            // Is the index valid?
            if (iIndex < 0 || iIndex >= m_IconControls.Count)
            {
                return;
            }

            // We simulate a click on the icon...
            IconControl ic = m_IconControls[iIndex];
            onIconClicked(ic, new IconControl.Args { ID = ic.ID });
        }


        #endregion

        #region Private functions
        
        /// <summary>
        /// Called when one of the icons is clicked.
        /// </summary>
        void onIconClicked(object sender, IconControl.Args e)
        {
            // When an Icon is clicked we:
            // a. Show a border around the selected one (and clear borders on the other icons)
            // b. Raise an event

            // a.
            foreach (IconControl ic in m_IconControls)
            {
                if(ic == sender)
                {
                    ic.BorderStyle = BorderStyle.FixedSingle;
                }
                else
                {
                    ic.BorderStyle = BorderStyle.None;
                }
            }
            
            // b.
            Utility.CrossThreadInvoke(SelectionChangedEvent, this, new Args { ID = e.ID });
        }

        #endregion

        #region Private data

        private const int SPACING = 5;
        private int m_iNextIconYPosition = SPACING;
        private IList<IconControl> m_IconControls = new List<IconControl>();

        #endregion
    }
}
